﻿using SharpCompress.Archives;
using SharpCompress.Archives.Rar;
using SharpCompress.Archives.Zip;
using SharpCompress.Common;
using SharpCompress.Writers;
using SharpCompress.Writers.Tar;
using SharpCompress.Writers.Zip;
using System.IO;
using System.Text;

namespace CompressDemo;

internal class SharpCompressDemo : ICompressDemo
{
    private readonly string _zipFileName = "sharpcompress.zip";
    private readonly string _tarFileName = "sharpcompress.gzip";
    private string TarFileFullName => Path.Combine(AppInfo.TargetDirectory, _tarFileName);
    private string ZipFileFullName => Path.Combine(AppInfo.TargetDirectory, _zipFileName);

    public Task Compress()
    {
        using (var archive = ZipArchive.Create())
        {
            /* 逐个文件添加
            archive.AddEntry("268-c1.bin", Path.Combine(AppInfo.SourceDirectory, "268-c1.bin"));
            archive.AddEntry("all01umd.map", Path.Combine(AppInfo.SourceDirectory, "all01umd.map"));
            archive.AddEntry("dd.txt", Path.Combine(AppInfo.SourceDirectory, "dd.txt"));            
            archive.AddAllFromDirectory(Path.Combine(AppInfo.SourceDirectory, "eshop"));
            */

            // 整个目录压缩
            archive.AddAllFromDirectory(AppInfo.SourceDirectory);
            archive.SaveTo(Path.Combine(AppInfo.TargetDirectory, _zipFileName), CompressionType.Deflate);
        }

        return Task.CompletedTask;
    }

    public Task CompressCHNFile()
    {
        WriterOptions opt = new(CompressionType.Deflate);
        //opt.ArchiveEncoding.CustomDecoder = (data, x, y) => Encoding.GetEncoding(932).GetString(data);
        //opt.ArchiveEncoding.Default = Encoding.GetEncoding("gbk");
        opt.ArchiveEncoding.Default = Encoding.UTF8;

        using (Stream outputStream = File.Create(ZipFileFullName))
        {
            using (IWriter writer = WriterFactory.Open(outputStream, ArchiveType.Zip, opt))
            {
                writer.WriteAll(AppInfo.SourceDirectory, "*", SearchOption.AllDirectories);
            }
        }
        return Task.CompletedTask;
    }

    public Task Decompress()
    {
        using (var archive = ZipArchive.Open(ZipFileFullName))
        {
            foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
            {
                entry.WriteToDirectory(Path.Combine(AppInfo.TargetDirectory, "新建文件夹"), new ExtractionOptions()
                {
                    ExtractFullPath = true,
                    Overwrite = true
                });
            }
        }
        return Task.CompletedTask;
    }

    public Task DecompressCHNFile()
    {
        var opts = new SharpCompress.Readers.ReaderOptions();
        var encoding = Encoding.GetEncoding(932);

        using (var archive = ZipArchive.Open(ZipFileFullName, opts))
        {
            foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
            {
                entry.WriteToDirectory(Path.Combine(AppInfo.TargetDirectory, "新建文件夹"), new ExtractionOptions()
                {
                    ExtractFullPath = true,
                    Overwrite = true
                });
            }
        }
        return Task.CompletedTask;
    }
}
